var DAO = require('./DAO')
//针对用户数据表操作的模块对象

var shareDAO = {
    //分享圈
    shareContent: function (callback) {
        DAO('select upmenu.*,users.userId,users.userName,users.headPic from upmenu,users where upmenu.touserId = users.userId order by upTime desc;', null, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //已发布内容
    publish: function (userId, callback){
        DAO('select upmenu.upmenuId,upmenu.upmenuTitle,upmenu.finishPic,upmenu.upTime,upmenu.story,users.userId,users.userName,users.headPic from upmenu join users on upmenu.touserId = users.userId where userId =? order by upTime desc', [userId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //分享圈详情
    sharedetail: function (upmenuId, callback) {
        DAO('select step.stepId,upmenu.upmenuTitle,step.stepPic,step.stepMake from upmenu join step on upmenu.upmenuId= step.toupmenuId where toupmenuId =?;', [upmenuId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //已发布内容的详情
    publishdetail: function (upmenuId, callback) {
        DAO('select upmenu.*,step.stepId,upmenu.upmenuTitle,step.stepPic,step.stepMake from upmenu join step on upmenu.upmenuId= step.toupmenuId where toupmenuId =?;', [upmenuId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },


    //上传菜谱new
    //插入步骤一
    upmenu:function(upmenuone,callback){
        DAO('insert into upmenu(upmenuTitle,finishPic,story,upTime,touserId) VALUES(?,?,?,now(),?)', [upmenuone.upmenuTitle,upmenuone.finishPic,upmenuone.story,upmenuone.touserId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    //查看步骤一的id值
    lookUpmenu:function(upmenuone,callback){
        DAO('select upmenuId,upmenuTitle,finishPic,story,upTime,touserId from upmenu where upmenuTitle=? and finishPic=? and story=? and touserId=? order by upmenuId desc LIMIT 1', [upmenuone.upmenuTitle,upmenuone.finishPic,upmenuone.story,upmenuone.touserId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    //插入步骤
    insertStep:function(stepmake,toupmenuId,callback){
        var arr=stepmake.step.split(',')
        var str='insert into step(stepPic,stepMake,toupmenuId) VALUES'
        var first='("'+arr[0]+'","'+arr[1]+'",?)'
        var option=''
        // var stepmake1=stepmake
        // for(var j=1;j<arr.length;j++){
        //     stepmake1 +=','+stepmake
        // }
        // var stepmake2=stepmake1.split(',')
        var toupmenuId1=toupmenuId
        for(var j=1;j<arr.length/2;j++){
            toupmenuId1 +=','+toupmenuId
        }
        var toupmenuId2=toupmenuId1.split(',')
        console.log(toupmenuId2)
        for(var i=1;i< arr.length/2;i++){
            option +=',("' + arr[2*i] + '","' + arr[2*i+1] + '",?)'
        }
        var sql=str+first+option
        console.log(sql)
        DAO(sql,toupmenuId2,function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    //若插入失败则删除上传菜谱步骤一
    delupmenu:function(toupmenuId,callback){
        DAO('delete from upmenu where upmenuId= ? ',[toupmenuId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
   

    //删除已发布菜谱2个.
   personalConment: function (upmenuId, callback) {
        DAO('delete from step where toupmenuId = ?', [upmenuId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    deletepersonal: function (upmenuId, callback) {
        DAO('delete from upmenu where upmenuId = ?', [upmenuId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
        //分享圈查新表
        selsharelike:function(user,callback){
            DAO('select toupmenuId from sharelike where touserId = ?',[user.phone],function(err,results){
                if(err){
                    callback(err,null)
                }else{
                    callback(null,results)
                }
            })
        },
        insertLike:function(upmenu,callback){
            DAO('insert into sharelike(toupmenuId,touserId) values(?,?)',[upmenu.upmenuId,upmenu.userId],
            function(err,results){
                if(err){
                    callback(err,null)
                }else{
                    callback(null,results)
                }
            })
        },
   
    //增加分享圈点赞数
    addUserlike: function (upmenuId, callback) {
        DAO('update upmenu set likeNum=likeNum+1 where upmenuId = ?', [upmenuId], function (err, results) {
            // console.log(results)
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
//    删除新表内容
    delLike:function(upmenu,callback){
        DAO('delete from sharelike where toupmenuId = ? and touserId = ?',[upmenu.upmenuId,upmenu.userId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    }, 
    //取消分享圈点赞数
    celUserlike: function (upmenuId, callback) {
        DAO('update upmenu set likeNum = likeNum-1 where upmenuId = ? ', [upmenuId], function (err, results) {
            console.log(results)
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    }
}
module.exports = shareDAO